<?php

/**
 * This is the model class for table "tb_user".
 *
 * The followings are the available columns in table 'tb_user':
 * @property string $id
 * @property string $username
 * @property string $password
 * @property string $created
 * @property string $modified
 */
class User extends CActiveRecord
{
        public $password_repeat;
        public $namaLengkap;
        public $email;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tb_user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ref_role_id, username, password, password_repeat', 'required'),
			array('username, password, password_repeat', 'length', 'max'=>100),
                        array('password_repeat', 'compare', 'compareAttribute'=>'password'),
			array('password_repeat, password', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('username, namaLengkap, email', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    'Role' => array(self::BELONGS_TO, 'Role', 'ref_role_id'),
                    'Profil' => array(self::HAS_ONE, 'Profil', 'tb_user_id'),
                    'User' => array(self::HAS_MANY, 'User', 'createdBy'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ref_role_id' => Yii::t('model', 'Level Otorisasi'),
			'username' => Yii::t('model', 'Username'),
			'password' => Yii::t('model', 'Password'),
			'password_repeat' => Yii::t('model', 'Ulangi Password'),
                        'namaLengkap' => Yii::t('model', 'Nama Lengkap'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
                
//              $criteria->compare('t.username',$this->username,true);
                $criteria->compare('Profil.namaLengkap',$this->namaLengkap,true);
                $criteria->compare('Profil.email',$this->email);
                $criteria->select = 't.id, t.ref_role_id, t.username, Profil.namaLengkap, Profil.email, Profil.tb_user_id';
                $criteria->join = 'LEFT JOIN tb_profil Profil ON (Profil.tb_user_id=t.id)';
                $criteria->order = 't.id DESC';
                
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
                        'sort'=>array(
                            'attributes'=>array(
                                'username',
                                'namaLengkap',
                                'email',
                            ),
                        ),
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function beforeSave() 
        {
            if($this->isNewRecord)
            {           
                 $this->created=new CDbExpression('NOW()'); 
                 $this->password = $this->hashPassword($this->password);
            }else{
                 $this->modified = new CDbExpression('NOW()'); 
                 if(!empty($this->password)){
                            $this->password = $this->hashPassword($this->password);
                    }
            }

            return parent::beforeSave();
        }

    /**
	 * Checks if the given password is correct.
	 * @param string the password to be validated
	 * @return boolean whether the password is valid
	 */
	public function validatePassword($password)
	{
		return CPasswordHelper::verifyPassword($password,$this->password);
	}

	/**
	 * Generates the password hash.
	 * @param string password
	 * @return string hash
	 */
	public static function hashPassword($password)
	{
		return CPasswordHelper::hashPassword($password);
	}
}
